Fly3D SDK

Tutorials

Home

Tutorial 12

Creating a front-end

· Introduction

After finishing this tutorial you will have created a new front-end executable. This front-end will be able to run any Fly3D level and is the simplest possible front end. It creates a window, sets full screen mode, initialises the engine, loads a level and starts the game

· Visual C++ operations

1) Open the Visual C++ Fly3D Workspace

2) Select the menu option File/New to create a new project

3) Select the projects tab and the Win32 Application type

4) Set the project location to the flysdk\frontend path and name the project 'flyfe'. Make sure to select 'add to curent workspace' to keep all Fly3D front-ends projects in a single workspace.

5) Select an empty project and add the following flyfe.cpp file to the project:

#include <windows.h>
#include <Fly3D.h>

char szTitle[100]="MyGame Title";
char szWindowClass[100]="MyGame";

// loads the menu level
void LoadLevel(HWND hWnd, HINSTANCE hInst)
{
    init_engine(hWnd,hInst,FLYAPPID_FLYFRONTEND);
    init_directx();
    init_render(FLY_RENDER_OPENGL);

    fullscreen=1;
    rend->SetFullScreen();

    flyengine->open_fly_file("menu/menu.fly");
    flyengine->init_texture_cache();
    InvalidateRect(hFlyWnd, 0, 0);
}

// main window message processing
LRESULT CALLBACK WinFunc (HWND hWnd, UINT mens, WPARAM wParam, LPARAM lParam)
{
    switch (mens)
    {
        // window resize
        case WM_SIZE:
            if (rend)
                rend->ResizeView(LOWORD(lParam),HIWORD(lParam));
            break;

        // window activation
        case WM_ACTIVATE:
            if (flyengine)
                if (LOWORD(wParam)==WA_INACTIVE || flyengine->con.mode)
                    flyengine->noinput=1;
                else
                    flyengine->noinput=0;
            break;

        // quit app
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
    }

    return DefWindowProc(hWnd, mens, wParam, lParam);
}

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lp, int nCmd)
{
    WNDCLASS wcl;
    MSG msg;

    // register window class
    wcl.style = CS_HREDRAW | CS_VREDRAW;
    wcl.lpfnWndProc = (WNDPROC)WinFunc;
    wcl.cbClsExtra = 0;
    wcl.cbWndExtra = 0;
    wcl.hInstance = hFlyInstance;
    wcl.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wcl.hCursor = 0;
    wcl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcl.lpszMenuName = NULL;
    wcl.lpszClassName = szWindowClass;
    if (!RegisterClass (&wcl))
    {
        MessageBox (0, "Can't register Window", "ERROR", MB_OK);
        return 0;
    }

    // cerate main window
    HWND hWndMain = CreateWindowEx(0,szWindowClass,szTitle,WS_POPUP,0, 0,
            GetSystemMetrics( SM_CXSCREEN ),GetSystemMetrics( SM_CYSCREEN ),
            NULL,NULL,hFlyInstance,NULL);

    // load the level
    ShowWindow (hWndMain, SW_MAXIMIZE);
    LoadLevel (hWndMain, hFlyInstance);

    // main loop
    while (1) 
    {
      while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE) 
      {
        if (GetMessage(&msg, NULL, 0, 0)) 
        {
          if (flyengine && (
             (msg.message==WM_KEYDOWN && msg.wParam==VK_ESCAPE) ||
             (msg.message==WM_CHAR && flyengine->con.mode && msg.wParam!=VK_ESCAPE)))
                 flyengine->con.key_press(msg.wParam);
          TranslateMessage(&msg);
          DispatchMessage(&msg);
        }
        else 
        {
          // frees engine, render, directx and quits app
          free_engine();
          free_render();
          free_directx();
          return TRUE;
        }
      }

      if (rend && flyengine)
      if (flyengine->step()) // simmulate
          rend->DrawView(); // draw view
    }
}

6) At the project settings change the follwing configurations in the Release and Debug versions:

- add the path '..\..\lib' to the 'Additional include directory' and 'Additional library path'.
- change the 'Output file name' to '..\..\flyfe.exe' so that the exe is placed with the other front-ends at the flysdk root path.
- set the 'Object/library modules' option to 'fly3d.lib user32.lib'

7) Compile and run the new front-end. It will set full screen and run the menu.fly level.